package com.enwei.activitys;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.enwei.adapters.AddressAdapter;
import com.enwei.base.BaseActivity;
import com.enwei.utils.CacheKeys;
import com.enwei.utils.Constans;
import com.enwei.utils.SpfHelper;
import com.enwei.utils.http.HttpApi;
import com.enwei.utils.http.HttpCallBack;
import com.ew.action.R;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class AddressActivity extends BaseActivity{
	
	private ListView mListView;
	private AddressAdapter mAdapter;
	private Button button;
	private JSONArray arrays = new JSONArray();
	public static JSONObject jsonObj = new JSONObject();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address,true,true);
		mListView = (ListView) findViewById(R.id.ordersList);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(AddressActivity.this, AddAddressActivity.class);
				intent.putExtra(AddAddressActivity.ADDRESS_ID_KEY, "");
				startActivityForResult(intent, 2000);
			}
		});
		initData();
	}

	public void initData(){
//		List<String> arrays = new ArrayList<String>();
//		for (int i = 0; i < 10; i++) {
//			arrays.add("sss"+i);
//		}
		mAdapter = new AddressAdapter(arrays, this);
		mListView.setAdapter(mAdapter);
		requestData();
	}
	
	public void requestData(){
		String url = Constans.ADDRESS_MANAGEMENT;
		String phone = (String) SpfHelper.getParam(this, CacheKeys.LOGIN_USER_HOME, "");
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("phone", phone));
		HttpApi http = new HttpApi(this,HttpMethod.POST);
		http.sendHttpRequest(url, nameValuePairs, new HttpCallBack() {
			
			@Override
			public void onSuccess(String data) {
				try {
					getLoadingView().setVisibility(View.GONE);
					if(!TextUtils.isEmpty(data)){
						arrays = new JSONArray(data);
						mAdapter.updateView(arrays);
						mListView.setVisibility(View.VISIBLE);
					}else{
						getEmpty().setVisibility(View.VISIBLE);
					}
				} catch (JSONException e) {
					getErrorLayout().setVisibility(View.VISIBLE);
					e.printStackTrace();
				}
				
			}
			@Override
			public void onFailure() {
				getLoadingView().setVisibility(View.GONE);
				getErrorLayout().setVisibility(View.VISIBLE);
				super.onFailure();
			}
		});
	}
	
	@Override
	protected void onRetry() {
		mListView.setVisibility(View.GONE);
		getLoadingView().setVisibility(View.VISIBLE);
		getErrorLayout().setVisibility(View.GONE);
		requestData();
		super.onRetry();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == 2000 && resultCode==RESULT_OK){
			requestData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}